home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cenvid9.zip / PATHSUBS.BAT < prev    next >
DOS Batch File  |  1994-03-04  |  9KB  |  235 lines

  1. @echo off
  2. REM ************************************************************************
  3. REM *** PathSubs.bat - Compact the PATH statement by using SUBST for the ***
  4. REM *** ver.1          paths in the list.  Also allow to add or delete a ***
  5. REM ***                path.  See the Instructions() function for a      ***
  6. REM ***                description of how to use                         ***
  7. REM ************************************************************************
  8.  
  9. subst | CEnvi %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
  10. GOTO CENVI_EXIT
  11.  
  12. Instructions()
  13. {
  14.    printf("\a\n")
  15.    printf("PathSubs - Keep the PATH environment variable as short as possible buy using\n")
  16.    printf("           DOS's SUBST command to replace PATH directories with drive letters.\n")
  17.    printf("\n")
  18.    printf("SYNTAX:  PathSubs [ < + | - > d:\\path ]\n")
  19.    printf("\n")
  20.    printf("Where:  +        Add a path to the PATH variable, if not already there.\n")
  21.    printf("        -        Remove a path from the PATH variable, if it is there.\n")
  22.    printf("        d:\\path  The FULL directory to add or remove from PATH.\n")
  23.    printf("\n")
  24.    printf("The following table shows examples of the SUBST command.\n");
  25.    printf("\n")
  26.    printf("PathSubs            Compacts the current path using drive substitutions\n")
  27.    printf("PathSubs + D:\\UTL   Add D:\UTL to the current PATH\n")
  28.    printf("PathSubs - D:\\UTL   Remove D:\UTL from the current PATH\n")
  29.    printf("\n")
  30. }
  31.  
  32. // The following code uses the path structure which has the elements:
  33. //   Count      : number of directories in the path
  34. //   path[Count].Dir   : array of Count strings, each containing a full directory
  35. //   path[Count].Drive : drive letter substituted here, else 0 for not subst
  36. // and also the Subs structure which has the elements
  37. //   Count        : number of drives Substituted
  38. //   sub[Count].Drive : array of Count drive letters substituted for Dir
  39. //   sub[Count].Dir   : array of Count strings for the path of each substituted driev
  40.  
  41. main(ArgCount,ArgStrings)
  42. {
  43.    // Decide what to do from input
  44.    if ( ArgCount == 3 ) {
  45.       Command = ArgStrings[1]
  46.       CommandPath = ArgStrings[2]
  47.       if ( Command[1] != 0  || ( Command[0] != '+'  &&  Command[0] != '-' ) ) {
  48.          // Invalid input, so just show how to use and quit
  49.          Instructions()
  50.          return(1)
  51.       }
  52.    } else if ( ArgCount != 1 ) {
  53.       // Invalid input, so just show how to use and quit
  54.       Instructions()
  55.       return(1)
  56.    }
  57.    // Build lists of substituted drives and of paths
  58.    Subs = BuildSubsArray()
  59.    Paths = SeparatePATHIntoDirs(Subs)
  60.    // implement new command based on input arguments
  61.    if ( ArgCount == 3 ) {
  62.       if ( Command[0] == '+' )
  63.          AddPath(CommandPath,Paths)
  64.       else
  65.          RemovePath(CommandPath,Paths,Subs)
  66.    }
  67.    CompactPaths(Paths,Subs)
  68.    ShowCurrentPath(Paths)
  69.    return(0)
  70. }
  71.  
  72. ShowCurrentPath(p)
  73. {
  74.    printf("PATH=")
  75.    for ( i = 0, count = 0; i < p.Count; i++ ) {
  76.       if ( p.path[i].Dir != NULL ) {
  77.          if ( count++ ) printf(";")
  78.          printf("%s",p.path[i].Dir)
  79.       }
  80.    }
  81.    printf("\n")
  82. }
  83.  
  84. AddPath(Dir,p)
  85. {
  86.    // First check if this directory is already in the path
  87.    for ( i = 0; i < p.Count; i++ ) {
  88.       if ( 0 == stricmp(Dir,p.path[i].Dir) ) {
  89.          printf("Directory \"%s\" is already in the PATH\n",p.path[i].Dir)
  90.          return
  91.       }
  92.    }
  93.    // was not found above in the path, and so add to end of path list
  94.    strcpy(p.path[p.Count].Dir,Dir)
  95.    p.path[p.Count].Drive = 0  // assume it is not substituted
  96.    p.Count++
  97. }
  98.  
  99. RemovePath(Dir,p,s)
  100. {
  101.    // First check that this directory is already in the path
  102.    for ( i = 0; i < p.Count; i++ ) {
  103.       if ( 0 == stricmp(Dir,p.path[i].Dir) ) {
  104.          break
  105.       }
  106.    }
  107.    if ( i == p.Count )
  108.       printf("Directory \"%s\" is not in the PATH\n",Dir)
  109.    else {
  110.       // remove this directory from the path, and remove its SUBST
  111.       if ( (drive = toupper(p.path[i].Drive)) != 0 ) {
  112.          // this path has a SUBSTITUTED drive, and so remove that subst drive
  113.          for ( j = 0; j < s.Count; j++ ) {
  114.             if ( s.sub[j].Drive == drive )
  115.                break;
  116.          }
  117.          s.sub[j].Drive = 0;
  118.          // use DOS's SUBST utility to un-substitute this drive
  119.          system("SUBST %c: /D",drive)
  120.       }
  121.       // set path structure to know this path is no longer there
  122.       p.path[i].Drive = 0;
  123.       p.path[i].Dir = NULL
  124.    }
  125. }
  126.  
  127. BuildSubsArray()  // Subst output was piped to this program, and so use that output
  128.                   // to build list of substituted drives and paths they point to.
  129.                   // output line is of the form "G: is substituted for C:\OS2"
  130. {
  131.    for ( s.Count = 0; NULL != (line=gets()); s.Count++ ) {
  132.       // first letter of SUBST is the drive letter
  133.       s.sub[s.Count].Drive = toupper(line[0]);
  134.       // last string before whitespace is the path
  135.       for ( d = line + strlen(line); d[-1] != ' '; d-- ) ;
  136.       strcpy(s.sub[s.Count].Dir,d)
  137.    }
  138.    return(s)
  139. }
  140.  
  141.  
  142. SeparatePATHIntoDirs(subs) // Separate the PATH environment variable into
  143.                            // individual paths, return element Count to how many
  144.                            // paths, and setting each Dir[] element to a path.
  145.                            // any paths that fit into the subst array will be
  146.                            // replaced by their full subst path
  147. {
  148.    Paths.Count = 0
  149.    p = PATH
  150.    while ( NULL != p  &&  0 != p[0] ) {
  151.       if ( ';' == p[0] )
  152.          p++   // skip semi-colons
  153.       else {
  154.          strcpy(NewDir,p)
  155.          if ( NULL != (c = strchr(NewDir,';')) )
  156.             c[0] = 0;
  157.          // check if this is a "A:\" type path and if it is substituted
  158.          Paths.path[Paths.Count].Drive = 0   // assume it is not substituted
  159.          if ( 0 == strcmp(NewDir+1,":\\") ) {
  160.             // loop through subst to see if this is one of those
  161.             for ( i = 0; i < subs.Count; i++ ) {
  162.                if ( toupper(NewDir[0]) == subs.sub[i].Drive ) {
  163.                   // put in the real path for this substituted one
  164.                   NewDir = subs.sub[i].Dir
  165.                   Paths.path[Paths.Count].Drive = subs.sub[i].Drive
  166.                   break
  167.                }
  168.             }
  169.          }
  170.          strcpy(Paths.path[Paths.Count++].Dir,NewDir)
  171.          p = strchr(p,';')
  172.       }
  173.    }
  174.    return(Paths)
  175. }
  176.  
  177. CompactPaths(p,s) // this writes the PATH statement in compacted form, substituting
  178.                   // all drives for a path whenever it can.  It assumes that it will
  179.                   // Subst on any drive letter that isn't used already.
  180. {
  181.    TempPath[0] = '\0'  // restart directories from scratch
  182.    SubstDir = "A:\\" // template for what a subdir looks like
  183.    for ( i = 0; i < p.Count; i++ ) {
  184.       if ( p.path[i].Dir != NULL ) {
  185.          if ( p.path[i].Drive != 0 ) {
  186.             // this path already has a drive letter substituted, and so keep it
  187.             SubstDir[0] = p.path[i].Drive
  188.             strcat(TempPath,SubstDir)
  189.          } else if ( strlen(p.path[i].dir) <= 3 ) {
  190.             // path can't get any shorter by substituting, and so keep it as it is
  191.                strcat(TempPath,p.path[i].Dir)
  192.          } else {
  193.             // get the next drive letter for substituting
  194.             drive = NextFreeDrive()
  195.             if ( drive != 0 ) {
  196.                // create a new SUBST drive for this path
  197.                system("SUBST %c: %s",drive,p.path[i].Dir)
  198.                SubstDir[0] = p.path[i].Drive = drive
  199.                strcat(TempPath,SubstDir)
  200.                // add this new drive to list of substituted drives
  201.                strcpy(s.sub[s.Count].Dir,p.path[i].Dir)
  202.                s.sub[s.Count].Drive = drive
  203.                s.Count++
  204.             } else {
  205.                // there are no more available free drives, and so simply add this dir
  206.                strcat(TempPath,p.path[i].Dir)
  207.             }
  208.          }
  209.          strcat(TempPath,";")
  210.       }
  211.    }
  212.    // remove the final semi-colon
  213.    TempPath[strlen(TempPath)-1